﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Fmis21_10.Models;

namespace Fmis21_10.Controllers
{
    public class TournamentTeamController : Controller
    {
        private FmisEntities db = new FmisEntities();

        //
        // GET: /TournamentTeam/

        public ActionResult Index()
        {
            var tournamentteams = db.TournamentTeams.Include(t => t.Team).Include(t => t.Tournament);
            return View(tournamentteams.ToList());
        }
        public ActionResult ViewIndex()
        {
            return View(db.TournamentTeams.ToList());
        }

        //
        // GET: /TournamentTeam/Details/5

        public ActionResult Details(int id = 0)
        {
            TournamentTeam tournamentteam = db.TournamentTeams.Find(id);
            if (tournamentteam == null)
            {
                return HttpNotFound();
            }
            return View(tournamentteam);
        }

        //
        // GET: /TournamentTeam/Create

        public ActionResult Create(int id=0)
        {
            Tournament tour = db.Tournaments.Find(id);
            ViewBag.TeamId = new SelectList(db.Teams, "TeamId", "TeamName");
            ViewBag.TournamentId = new SelectList(db.Tournaments, "TournamentId", "TournamentName");
            if (tour == null)
            {

            }
            else
            {
                ViewBag.TournamentId = new SelectList(db.Tournaments, "TournamentId", "TournamentName", tour.TournamentId);
            }
            return View();
        }

        //
        // POST: /TournamentTeam/Create

        [HttpPost]
        public ActionResult Create(TournamentTeam tournamentteam, Rating rat)
        {
            if (ModelState.IsValid)
            {
                db.TournamentTeams.Add(tournamentteam);
                
                db.SaveChanges();
                rat.Draw = 0;
                rat.Lose = 0;
                rat.Point = 0;
                rat.TeamId = tournamentteam.TeamId;
                rat.TournamentId = tournamentteam.TournamentId;
                rat.Win = 0;
                db.Ratings.Add(rat);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            
            ViewBag.TeamId = new SelectList(db.Teams, "TeamId", "TeamName", tournamentteam.TeamId);
            ViewBag.TournamentId = new SelectList(db.Tournaments, "TournamentId", "TournamentName", tournamentteam.TournamentId);
            return View(tournamentteam);
        }

        //
        // GET: /TournamentTeam/Edit/5

        public ActionResult Edit(int id = 0)
        {
            TournamentTeam tournamentteam = db.TournamentTeams.Find(id);
            if (tournamentteam == null)
            {
                return HttpNotFound();
            }
            ViewBag.TeamId = new SelectList(db.Teams, "TeamId", "TeamName", tournamentteam.TeamId);
            ViewBag.TournamentId = new SelectList(db.Tournaments, "TournamentId", "TournamentName", tournamentteam.TournamentId);
            return View(tournamentteam);
        }

        //
        // POST: /TournamentTeam/Edit/5

        [HttpPost]
        public ActionResult Edit(TournamentTeam tournamentteam)
        {
            if (ModelState.IsValid)
            {
                db.Entry(tournamentteam).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.TeamId = new SelectList(db.Teams, "TeamId", "TeamName", tournamentteam.TeamId);
            ViewBag.TournamentId = new SelectList(db.Tournaments, "TournamentId", "TournamentName", tournamentteam.TournamentId);
            return View(tournamentteam);
        }

        //
        // GET: /TournamentTeam/Delete/5

        public ActionResult Delete(int id = 0)
        {
            TournamentTeam tournamentteam = db.TournamentTeams.Find(id);
            if (tournamentteam == null)
            {
                return HttpNotFound();
            }
            return View(tournamentteam);
        }

        //
        // POST: /TournamentTeam/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            TournamentTeam tournamentteam = db.TournamentTeams.Find(id);
            db.TournamentTeams.Remove(tournamentteam);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}